<?php 
include_once 'include.php';
$reminder = new reminder();
$template = new template("reminder");
$menu = menu();
$use = $db->get_row("Select * from Users where id='".$_SESSION['id']."'");
if(isLoggedIn())
{
$template->setTitle("Reminder");
$page = "list";
$id = 0;
$typeId = 0;
$message = null;
if(isset ($_GET["page"]))$page = $_GET["page"];
if(isset ($_GET["id"]))$id = $_GET["id"];
if(isset ($_GET["typeid"]))$typeId = $_GET["typeid"];
//if(isset ($_GET["message"]))$message = $_GET["message"];

$type =0;
if(isset ($_GET["type"]))$type = $_GET["type"];
if(isLoggedIn())
{
    switch ($page)
    {
        case "reminder":
            $template->addContent($donations->addDonation($id));
        break;    
    case "addReminder":
        $template->addContent($reminder->addReminder($type, $typeId));
        break;
    
    case "added":
        $reminder->added();
        break;
    
     case "editReminder":
        $template->addContent($reminder->editReminder($id, $typeId));
        break;
    
    case "updated":
        $reminder->updated();
        break;
    
    case "delete":
        $reminder->deleteReminder($id, $typeId);
        break;
        default:
            break;
    }
}
else
{ 
    $template->addContent("What are you doing here ??");
}
    $template->showMenu($menu);
    $template->showLogin("Welcome");
    $template->showUser($use->name.' | <a href="logout.php" class="logout">Logout</a>');
$template->genPage();
}

else
    header("Location:login.html");
class reminder
{
    function listreminders()
    {
        global $db;
        $row = $db->get_row("SELECT * FROM reminder");
        $reminders = $db->get_results("SELECT * from reminder");
        $data = "<h2>Upcoming Reminders</h2>";

        foreach($reminders as $rem)
        {
            //$data .= $rem->id." | ";
            $data .= $rem->date."<br> | ";
            $data .= getReminderName($rem->type)."<br> | ";
            //$data .= $rem->typeId." | ";
            $data .= getReminderStatusName($rem->status)."<br> | ";
            $data .= $rem->comment."<br> | ";
            $data .= getReminderPriorityName($rem->priority)."<br> | ";
            $data .= getReminderRepeatName($rem->repeat);
            $data .= "<br><br>";
        }
        return $data;
    }    
    
    
    
        
    function added()
    {
        global $db;
       
       $type = $_POST['type']; 
       $typeId = $_POST['typeid'];
       $date = $_POST['date']; 
       $comment = $_POST['comment']; 
       $priority = $_POST['prior'];
       $repeat = $_POST['repeat'];
    $status = $_POST['status'];
     // var_dump($_POST); die();
       
       //$query = "Insert into reminder (type, typeID, date, comment, status, priority, repeat) VALUES ('".$type."','".$typeId."','".$date."' ,'".$comment."', ".$status." , ".$priority.", ".$repeat.")";
       $query2 = "INSERT INTO `drm_db`.`reminder` (`id`, `type`, `typeId`, `date`, `comment`, `status`, `priority`, `repeat`) VALUES (NULL, '$type', '$typeId', '$date', '$comment', '$status', '$priority', '$repeat');";
      // echo $query."<br/>";
      // echo $query2; //die();
       $db->query($query2);
       if($type != 1)
       {
           if($type == 2) $typeId = $db->get_var("SELECT `donorId` FROM `donations` WHERE `id` = $typeId");
           else $typeId =  $db->get_var("SELECT `donorId` FROM `communications` WHERE `id` = $typeId");
       }
       
       header("Location:donor.php?page=item&id=".$typeId."&message=Reminder Added");
       
    }
    
 function addReminder($type, $typeID)
    {
        $page = "<div class='form'>
         <form action='reminder.php?page=added' method='post' class='niceform'>
         
                <fieldset>
                
<input type= 'hidden' name = 'type' value='".$type."'/>
    <input type= 'hidden' name = 'typeid' value='".$typeID."'/>
                    <dl>
                        <dt><label for='date'>Date:</label></dt>
                        <dd><input id='datepicker' type='text' name='date'></dd>
                    </dl>
                    
                    <dl>
                        <dt><label for='comment'>Comment:</label></dt>
                        <dd><textarea name='comment' id='' size='54' value= ''></textarea></dd>
                    </dl>    
                    <dl>
                        <dt><label for='status'>Select status:</label></dt>
                        <dd>
                            <select size='1' name='status' id=''>
                                <option value='1' >None</option>
                                <option value='2' >Opened</option>
                                <option value='3' >Completed</option>
                                
                            </select>
                        </dd>
                    </dl>
                    
                    <dl>
                        <dt><label for='prior'>Select Priority:</label></dt>
                        <dd>
                            <select size='1' name='prior' id=''>
                                <option value='1' >None</option>
                                <option value='2' >Low</option>
                                <option value='3' >Medium</option>
                                <option value='4' >High</option>
                                <option value='5' >Urgent</option>
                            </select>
                        </dd>
                    </dl>
                    
                    <dl>
                        <dt><label for='repeat'>Select repeat:</label></dt>
                        <dd>
                            <select size='1' name='repeat' id=''>
                                <option value='1' >None</option>
                                <option value='2' >Week</option>
                                <option value='3' >Month</option>
                                <option value='4' >Year</option>
                         
                            </select>
                        </dd>
                    </dl>
                    
                       
                
                 
                    ";
                    $page .= "<dl class='submit'>
                    <input type='submit' name='submit' id='submit' value='Add' />
                     </dl>
                    
                </fieldset>
                
         </form>
         </div>  ";     
        
        return $page;       
    }
 
    
    
    
    
     function updated()
    {
        global $db;
       
       $id = $_POST['id'];
       $typeId = $_POST['typeId'];
       $date = $_POST['date']; 
       $comment = $_POST['comment']; 
       $priority = $_POST['prior'];
       $repeat = $_POST['repeat'];
    $status = $_POST['status'];
     // var_dump($_POST); die();
       
       //$query = "Insert into reminder (type, typeID, date, comment, status, priority, repeat) VALUES ('".$type."','".$typeId."','".$date."' ,'".$comment."', ".$status." , ".$priority.", ".$repeat.")";
      // $query2 = "INSERT INTO `drm_db`.`reminder` (`id`, `type`, `typeId`, `date`, `comment`, `status`, `priority`, `repeat`) VALUES (NULL, '$type', '$typeId', '$date', '$comment', '$status', '$priority', '$repeat');";
      $query2 = "UPDATE `reminder` SET `date`='$date',`comment`='$comment',`status`='$status',`priority`='$priority',`repeat`='$repeat' WHERE id = ".$id;
      // 
      /// echo $query."<br/>";
      // echo $query2; die();
       $db->query($query2);
       
       if($type != 1)
       {
           if($type == 2) $typeId = $db->get_var("SELECT `donorId` FROM `donations` WHERE `id` = $typeId");
           else $typeId =  $db->get_var("SELECT `donorId` FROM `communications` WHERE `id` = $typeId");
       }
       header("Location:donor.php?page=item&id=".$typeId."&message=Reminder Updated");
       
    }
    
 function editReminder($id, $typeID)
    {
     
     global $db;
        
        $reminder = $db->get_row('Select * from reminder where id ='.$id);
        $selectedStat = array("","","");
        $selectedStat[$reminder->status -1] = "Selected='selected'";
        
        $selectedPrior = array("","","","","");
        $selectedPrior[$reminder->priority -1] = "Selected='selected'";
        
        $selectedRepeat = array("","","","");
        $selectedRepeat[$reminder->repeat -1] = "Selected='selected'";
        
        $page = "<div class='form'>
         <form action='reminder.php?page=updated' method='post' class='niceform'>
         
                <fieldset>
                
<input type= 'hidden' name = 'id' value='".$id."'/>
    <input type= 'hidden' name = 'typeId' value='".$typeID."'/>
                    <dl>
                        <dt><label for='date' >Date:</label></dt>
                        <dd><input id='datepicker' value='$reminder->date' type='text' name='date'></dd>
                    </dl>
                    
                    <dl>
                        <dt><label for='comment' >Comment:</label></dt>
                        <dd><textarea name='comment' id='' size='54' value= ''>$reminder->comment</textarea></dd>
                    </dl>    
                    <dl>
                        <dt><label for='status'>Select status:</label></dt>
                        <dd>
                            <select size='1' name='status' id=''>
                                <option value='1' ".$selectedStat[0].">None</option>
                                <option value='2' ".$selectedStat[1].">Opened</option>
                                <option value='3' ".$selectedStat[2].">Completed</option>
                                
                            </select>
                        </dd>
                    </dl>
                    
                    <dl>
                        <dt><label for='prior'>Select Priority:</label></dt>
                        <dd>
                            <select size='1' name='prior' id=''>
                                <option value='1' ".$selectedPrior[0].">None</option>
                                <option value='2' ".$selectedPrior[1].">Low</option>
                                <option value='3' ".$selectedPrior[2].">Medium</option>
                                <option value='4' ".$selectedPrior[3].">High</option>
                                <option value='5' ".$selectedPrior[4].">Urgent</option>
                            </select>
                        </dd>
                    </dl>
                    
                    <dl>
                        <dt><label for='repeat'>Select repeat:</label></dt>
                        <dd>
                            <select size='1' name='repeat' id=''>
                                <option value='1' ".$selectedRepeat[0].">None</option>
                                <option value='2'  ".$selectedRepeat[1].">Week</option>
                                <option value='3'  ".$selectedRepeat[2].">Month</option>
                                <option value='4'  ".$selectedRepeat[3].">Year</option>
                         
                            </select>
                        </dd>
                    </dl>
                    
                       
                
                 
                    ";
                    $page .= "<dl class='submit'>
                    <input type='submit' name='submit' id='submit' value='Update' />
                     </dl>
                    
                </fieldset>
                
         </form>
         </div>  ";     
        
        return $page;       
    }
    
     function deleteReminder($id, $typeId)
    {
        Global $db;
        $type = $db->get_var("Select type from reminder where id =".$id);
        $db->query("DELETE FROM reminder WHERE id = ".$id);
    
        if($type != 1)
       {
           if($type == 2) $typeId = $db->get_var("SELECT `donorId` FROM `donations` WHERE `id` = $typeId");
           else $typeId =  $db->get_var("SELECT `donorId` FROM `communications` WHERE `id` = $typeId");
       }
        header("Location:donor.php?page=item&id=$typeId&message=Donor Deleted");
    }
}
?>
